package com.cc.controller;  
  
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;  
import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;  
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.cc.entity.Institute;
import com.cc.entity.Major;
import com.cc.entity.User;
import com.cc.entity.UserSchool;
import com.cc.mongo.TUser;
import com.cc.service.FnService;
import com.cc.utils.AutherUtils;  
  
@Controller  
@RequestMapping("/fn")  
public class FnController {  
	@Resource  
	protected FnService fnservice;
	
	@ResponseBody
    @RequestMapping("/getSchoolList")  
    public String getSchoolList(HttpServletRequest request,Model model){  
		List<UserSchool> data=fnservice.getSchoolList();
		ObjectMapper jsonmapper=new ObjectMapper();
		String result="";
		try {
			result=jsonmapper.writeValueAsString(data);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return result;  
    }
	
	
	@ResponseBody
    @RequestMapping("/getInsList")  
    public String getInsList(HttpServletRequest request,Model model,UserSchool school){
		List<Institute> data=fnservice.selectBySchoolid(school.getId());
		ObjectMapper jsonmapper=new ObjectMapper();
		String result="";
		try {
			result=jsonmapper.writeValueAsString(data);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
	@ResponseBody
    @RequestMapping("/getMajorList")  
    public String getMajorList(HttpServletRequest request,Model model,Institute ins){
		List<Major> data=fnservice.selectByinsid(ins.getId());
		ObjectMapper jsonmapper=new ObjectMapper();
		String result="";
		try {
			result=jsonmapper.writeValueAsString(data);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	@ResponseBody
    @RequestMapping("/getCTX")  
    public String getCTX(HttpServletRequest request,Model model,UserSchool school){
		String result=AutherUtils.getAppContext(request);
		return result;
	}
}